﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using ByteSite.Common;

namespace ByteSite.BLL
{
    /// <summary>
    ///订单表
    /// </summary>
    public partial class orders
    {
        private readonly Entitys.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.Data data;

        public orders()
        {
            data = new DAL.Data();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return data.Exists<Entitys.dt_orders>(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Entitys.dt_orders model)
        {
            try
            {
                data.BeginTran();
                model.id = data.Insert(model);
                if (model.order_goods != null)
                {
                    if (model.order_goods.Count > 0)
                    {
                        foreach (Entitys.dt_order_goods goods in model.order_goods)
                        {
                            goods.order_id = model.id;
                            data.Insert<Entitys.dt_order_goods>(model.order_goods);
                        }
                    }
                }

                data.CommitTran();
            }
            catch(Exception ex)
            {
                data.RollbackTran();
                return 0;
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Entitys.dt_orders model)
        {
            //计算订单总金额:商品总金额+配送费用+支付手续费
            //model.order_amount = model.real_amount + model.express_fee + model.payment_fee + model.invoice_taxes;
            return data.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            try
            {
                data.BeginTran();
                data.Delete<Entitys.dt_order_goods>(x => x.order_id == id);
                data.Delete<Entitys.dt_orders>(id);
                data.CommitTran();
            }
            catch (Exception ex)
            {
                data.RollbackTran();
                return false;
            }
            return true;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Entitys.dt_orders GetModel(int id)
        {
            Entitys.dt_orders model = data.Get<Entitys.dt_orders>(id);
            model.order_goods = new List<Entitys.dt_order_goods>();
            model.order_goods.AddRange(data.List<Entitys.dt_order_goods>(x => x.order_id == id));
            return model;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Entitys.dt_orders> GetList(int top, string strWhere, string filedOrder)
        {
            return data.List<Entitys.dt_orders>(top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public List<Entitys.dt_orders> GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {

            return data.List<Entitys.dt_orders>(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string order_no)
        {
            return data.Exists<Entitys.dt_orders>(x => x.order_no == order_no);
        }

        /// <summary>
        /// 根据订单号返回一个实体
        /// </summary>
        public Entitys.dt_orders GetModel(string order_no)
        {
            Entitys.dt_orders model = data.Get<Entitys.dt_orders>(x => x.order_no == order_no);
            model.order_goods = new List<Entitys.dt_order_goods>();
            model.order_goods.AddRange(data.List<Entitys.dt_order_goods>(x => x.order_id == model.id));
            return model;
        }

        /// <summary>
        /// 根据订单号获取支付方式ID
        /// </summary>
        public int GetPaymentId(string order_no)
        {
            return Utils.ObjToInt(data.Get<Entitys.dt_orders>(x => x.order_no == order_no).payment_id);
        }

        /// <summary>
        /// 返回数据数
        /// </summary>
        public int GetCount(Expression<Func<Entitys.dt_orders, bool>> whereExpression)
        {
            return data.Count<Entitys.dt_orders>(whereExpression);
        }

        public int GetCount(string strWhere)
        {
            return data.Count<Entitys.dt_orders>(strWhere);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(Expression<Func<Entitys.dt_orders, Entitys.dt_orders>> expression, Expression<Func<Entitys.dt_orders, bool>> whereExpression)
        {
            return data.UpdateColumns<Entitys.dt_orders>(expression, whereExpression);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(Expression<Func<Entitys.dt_orders, bool>> expression, Expression<Func<Entitys.dt_orders, bool>> whereExpression)
        {
            return data.UpdateColumns<Entitys.dt_orders>(expression, whereExpression);
        }
        #endregion
    }
}